package com.xpsoft.oa.service.project.impl;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.oa.dao.project.ContractDao;
import com.xpsoft.oa.dao.project.ProjectDao;
import com.xpsoft.oa.dao.project.ProjectPaymentDao;
import com.xpsoft.oa.model.project.Contract;
import com.xpsoft.oa.model.project.Project;
import com.xpsoft.oa.model.project.ProjectPayment;
import com.xpsoft.oa.service.project.ProjectPaymentService;

public class ProjectPaymentServiceImpl extends BaseServiceImpl<ProjectPayment> implements ProjectPaymentService{
	private ProjectPaymentDao dao;
	@Autowired
	private ProjectDao projectDao;
	@Autowired
	private ContractDao contractDao;
	public ProjectPaymentServiceImpl(ProjectPaymentDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public Map<String, Object> getPayed(String proId,String suppliersId,String contractId) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isNotBlank(proId)&&StringUtils.isNotBlank(suppliersId)&&StringUtils.isNotBlank(contractId)){
			Long projectId = Long.valueOf(proId);
			Long supId = Long.valueOf(suppliersId);
			Contract contract = contractDao.get(Long.valueOf(contractId));
			BigDecimal contractAmount = contract.getContractAmount();
			BigDecimal payedAmount = dao.getPayedByProId(projectId,supId);
			BigDecimal payedPercentage = payedAmount.multiply(new BigDecimal(100)).divide(contractAmount,2,RoundingMode.HALF_DOWN);
			map.put("payedPercentage", payedPercentage);
			map.put("payedAmount", payedAmount);
		}else{
			map.put("payedPercentage", 0);
			map.put("payedAmount", 0);
		}
		return map;
	}

	
}